/**
******************************************************************************************
* @file MercureLover.c
* @brief Management of the temperature bricklet.
* @date 29 mars 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "MercureLover.h"

/*******************************************************************************************
* PROTOTYPES
********************************************************************************************/

void cb_temperature(int16_t, void *);

void MercureLover_initThreshold(MercureLover *);

void MercureLover_destroyUtilities(MercureLover*);

void MercureLover_destroy(MercureLover**);

int MercureLover_registerObserver(MercureLover*, Observer*);

int MercureLover_unregisterObserver(MercureLover*, Observer*);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @param ipcon Ipconnection for link bricklet to the rest of bricks/bricklets
 * @return Pointer to a new MercureLover
 **/
MercureLover* MercureLover_create(IPConnection ipcon){
	MercureLover* This = (MercureLover *) malloc(sizeof(*This));

	//Creation of temperature sensor
	Temperature t_sensor;
	temperature_create(&t_sensor, "qJx", &ipcon);
	This->temperatureSensor = t_sensor;

        //Creation of the mercureController and averageMaker
        This->mercureController = MercureController_create();
        //0 is for temperature 1 is for humidity
        This->averageMaker = AverageMaker_create(0);

        //Init functions
	This->initThreshold=MercureLover_initThreshold;
        This->destroy = MercureLover_destroy;
	This->subject = subjectNew(This, 1);
	This->registerObserver = MercureLover_registerObserver;
	This->unregisterObserver = MercureLover_unregisterObserver;

        //Init variable
        This->temperature = 0;

	return This;
}

/**
 * @brief Use by the callback function for notify observers and MercureController with the good temperature if it changed.(+ or - 0.1°C)
 * @param temp Value of the temperature given by the bricklet
 * @param user_data Pointer to the wanted MercureLover
 **/
void cb_temperature(int16_t temp, void *user_data) {

	MercureLover* This;
	This = (MercureLover*) user_data;

        //Calculate the average of temperature
        This->averageMaker->averageMAJ(This->averageMaker,temp/100.0);

        //Notify observers when the temperature changed (+ or - 0.1°C ).Warn MercureController too
        if ( (((This->temperature * 100) +10) < temp) || (((This->temperature * 100) -10) > temp)){
		This->temperature = temp/100.0;
                //Give the current temperature to Mercure controller for check if it's a warning temperature
                This->mercureController->defineCriticality(This->mercureController,This->temperature);
		This->subject->notifyObservers(This->subject);

	}
}


/**
 * @brief Init  MercureLover, the AverageMaker associate, the MercureController associate and callback function
 * @param This Pointer to the wanted MercureLover
 **/
void MercureLover_initThreshold(MercureLover *This){
        // Create the callback. It's called every second
	//temperature_set_temperature_callback_period(&This->temperatureSensor, 1000);
        temperature_set_debounce_period(&This->temperatureSensor, 1000);

	// Register temperature callback to function cb_temperature
	temperature_register_callback(&This->temperatureSensor,
	                              TEMPERATURE_CALLBACK_TEMPERATURE_REACHED,
	                              (void *)cb_temperature,
	                              (void *) This);

	// Configure threshold for temperature is inside -39°C / 124°c
	temperature_set_temperature_callback_threshold(&This->temperatureSensor, 'i', -39*100, 124*100);

        //Recover the first temperature
        int16_t temp;
        temperature_get_temperature(&This->temperatureSensor, &temp);
        This->temperature = temp/100.0;
	This->mercureController->defineCriticality(This->mercureController,This->temperature);
        This->averageMaker->init(This->averageMaker,This->temperature);

}

/**
 * @brief Destroy all of the components of MercureLover
 * @param This Pointer to the wanted MercureLover
 **/
void MercureLover_destroyUtilities(MercureLover* This)
{
	This->subject->destroy(This->subject);

	This->mercureController->destroy(&This->mercureController);
        This->averageMaker->destroy(&This->averageMaker);
        temperature_destroy(&This->temperatureSensor);
}

/**
 * @brief Destructor
 * @param This Double pointer to the wanted MercureLover
 **/
void MercureLover_destroy(MercureLover** This)
{
        MercureLover_destroyUtilities(* This);

	if (*This) {
		free(*This);
		*This = NULL;
	}
}


/**
 * @brief Proxy method for Register an observer for the MercureLover object.
 * @param This Pointer to the wanted MercureLover
 * @param This Pointer to the wanted observer
 * @return The result of the development of the registration (0 = GOOD and 1 = NOT GOOD)
 **/
int MercureLover_registerObserver(MercureLover* This, Observer* observer) {
	return This->subject->registerObserver(This->subject, observer);
}


/**
 * @brief Proxy method for Unregister observer for the MercureLover object.
 * @param This Pointer to the wanted MercureLover
 * @param This Pointer to the wanted observer
 * @return The result of the development of the unregistration (0 = GOOD and 1 = NOT GOOD)
 **/
int MercureLover_unregisterObserver(MercureLover* This, Observer* observer) {
	return This->subject->unregisterObserver(This->subject, observer);
}
